<?php
class Vote extends AppModel {
	var $name = 'Vote';
	var $validate = array(
		'link_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'value' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ip_int' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Link' => array(
			'className' => 'Link',
			'foreignKey' => 'link_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	function getVoteFreq($user){
		$conditions=array('user_id'=>$user['id']);
		if($user['id']==0) $conditions['ip_int']=Visitor::ip_int();	
		$conditions['type']='links';
		$conditions[]='created > subtime(now(), \'0:0:30\')';

		return $this->find('count',array('conditions'=>$conditions,'recursive'=>-1));
	}
	
	function getLinkVoteValue($link_id,$user_id=0,$check_ip=false){
		$conditions=array('type'=>'links','link_id'=>$link_id);
		
		if ($user_id==0){
			$conditions['ip_int']=Visitor::ip_int();
		}else {
			if ($check_ip) $conditions['ip_int']=$ip;
			$conditions['OR']=array('user_id'=>$user_id,'ip_int'=>Visitor::ip_int());
		}
		
		$vote=($this->find('first',array('fields'=>'value','recursive'=>-1,'conditions'=>$conditions)));
		return ($vote)? $vote['Vote']['value']:0;		
	}
	
	function userHasVote($link_id,$user_id,$check_ip=false){				
		return ($this->getLinkVoteValue($link_id,$user_id,$check_ip)!=0);
	}
	
	function countLinkVoteClons($link_id,$user_id,$days){
		return $this->countClons('links', $link_id, $user_id,'Vote.user_id',$days);	
	}
	
	
	function countClons($type,$link_id,$user_id,$to,$days=7){
		$c = $this->find('count',array(
			'conditions'=>array(
				'Vote.type'=>$type,
				'Vote.link_id' => $link_id,
			),'joins'=>array(
				array(
					'type'=>'INNER',
					'table' => 'clons',
            		'alias' => 'Clon',
            		'conditions'=> array(
						'Clon.from' => $user_id,
						'Clon.to' => $to, 
						'Clon.created' => 'date_sub(now(), interval '.$days.' day)',
						'Clon.ip like'=>'COOK:%')				
				)
			)
		));
		
		return $c;
	}
	
	
}
?>